
namespace Nutils
{
    public class Helper
    {
        public delegate void SetString(string s);
        public string About;

        private System.Windows.Forms.ToolTip _toolTip;
        private SetString _setString;
        private ControlDataDict _controls;

        public Helper(string sHelpFile, SetString setFunc, System.ComponentModel.IContainer components)
        {
            this._toolTip = new System.Windows.Forms.ToolTip(components);
            this._toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            this._toolTip.Popup += new System.Windows.Forms.PopupEventHandler(this._ToolTip_Popup);

            _setString = new SetString(setFunc);
            About = "";
            _controls = new ControlDataDict();
            ReadConfig(sHelpFile);
        }

        public void RegisterControl(System.Windows.Forms.Control control)
        {
            if (!_controls.ContainsKey(control.Name))
            {
                Nutils.Console.Spam("No help info for: " + control.Name);
                return;
            }

            string shortTip = _controls[control.Name].ShortTip;
            if (0 >= shortTip.Length)
            {
                _toolTip.SetToolTip(control, ":Dummy Tip:");
                return;
            }

            _toolTip.SetToolTip(control, shortTip);
        }

        private void _ToolTip_Popup(object sender, System.Windows.Forms.PopupEventArgs e)
        {
            if (!_controls.ContainsKey(e.AssociatedControl.Name))
            {
                return;
            }

            ControlData data = _controls[e.AssociatedControl.Name];
            _setString(data.LongTip);

            if( 0 >= data.ShortTip.Length )
            {
                e.Cancel = true;
            }
        }

        public void ReadConfig(string configFile)
        {
            if (!System.IO.File.Exists(configFile))
                return;

            XML.Reader cReader = XML.OpenReader(configFile);
            if (null == cReader)
            {
                return;
            }

            ControlData controlData = null;

            while (cReader.Read())
            {
                if (cReader.Name.Equals("HelpTip"))
                {
                    switch (cReader.NodeType)
                    {
                        case System.Xml.XmlNodeType.EndElement:

                            if (!_controls.ContainsKey(controlData.ControlName))
                                _controls.Add(controlData.ControlName, controlData);
                            else
                                _controls[controlData.ControlName] = controlData;
                            
                            controlData = null;
                            break;
                        case System.Xml.XmlNodeType.Element:
                            controlData = new ControlData();
                            break;
                    }
                }
                else if (controlData == null)
                {
                    if (cReader.LocalName.Equals("About"))
                    {
                        About = cReader.ReadString();
                    }
                }
                else if (cReader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    controlData.Parse(cReader);
                }
            }
        }

        class ControlData
        {
            public string ControlName;
            public string ShortTip;
            public string LongTip;

            public ControlData()
            {
                ControlName = "";
                ShortTip = "";
                LongTip = "";
            }

            public void Parse(XML.Reader cReader)
            {
                if (cReader.LocalName.Equals("ControlName"))
                {
                    ControlName = cReader.ReadString();
                }

                else if (cReader.LocalName.Equals("Short"))
                {
                    ShortTip = cReader.ReadString();
                }

                else if (cReader.LocalName.Equals("Long"))
                {
                    LongTip = cReader.ReadString();
                }
            }
        }
        class ControlDataDict : System.Collections.Generic.Dictionary<string, ControlData> { }
    }
}















